package demo.uhf.com.androiduhf;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final String TAG = MainActivity.class.getSimpleName();

    Button mBtnStart;
    TextView mTvData;
    ScrollView mScrollView;

    StringBuilder mBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initListener();
        initData();
    }

    private void initView() {
        mScrollView = (ScrollView) findViewById(R.id.scrollView);
        mBtnStart = (Button) findViewById(R.id.btn_start);
        mTvData = (TextView) findViewById(R.id.tv_data);

        initButton();
    }

    private void initButton() {
        if (UhfHelper.getInstance().isLooper()) {
            mBtnStart.setText("停止查找");
        } else {
            mBtnStart.setText("启动查找");
        }
    }


    private void initListener() {
        mBtnStart.setOnClickListener(this);
        findViewById(R.id.btn_setting).setOnClickListener(this);
    }

    private void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                if (UhfHelper.getInstance().isLooper()) {
                    stopLooper();
                } else {
                    startLooper();
                }
                break;
            case R.id.btn_setting:
                Intent intent = new Intent(this, SettingActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void startLooper() {
        UhfHelper.getInstance().startLooper(new UhfHelper.IUhfListener() {
            @Override
            public void doInventory(String data) {
                showData(data);
            }
        });
        initButton();
    }

    private void stopLooper() {
        UhfHelper.getInstance().setLooper(false);
        initButton();
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopLooper();
    }

    private void showData(String data) {
        if (mBuilder == null) {
            mBuilder = new StringBuilder();
        }
        mBuilder.append(data);
        mBuilder.append("\n");

        mTvData.setText(mBuilder.toString());

        mScrollView.post(new Runnable() {
            @Override
            public void run() {
                mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopLooper();
        UhfHelper.getInstance().closeHuf();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        stopLooper();
        UhfHelper.getInstance().closeHuf();
        System.exit(0);
    }
}
